package vn.gq.udv.db;

import java.util.ArrayList;
import java.util.List;

import vn.gq.tym.storytemplate.GQConst;
import vn.gq.udv.db.items.Category;
import vn.gq.udv.db.items.ChapterDetail;
import vn.gq.udv.db.items.ChapterItem;
import vn.gq.udv.utils.AESCipher;
import android.content.Context;
import android.database.Cursor;

public class DbAdapter extends DatabaseHelper {

	public static final String TABLE_STORY = "vn_gq_udv_story";
	public static final String TABLE_CATEGORY = "vn_gq_udv_category";
	
	private AESCipher cipher;
	
	public DbAdapter(Context context) {
		super(context);
		cipher = new AESCipher("longhoanggiang");
	}
	
	public String decrypt(String input) {
		String result = input;
		if (GQConst.DB.ENCRYPTED) {
			try {
				result = cipher.decrypt(input);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return result;
	}
	
	@Override
	public List<ChapterItem> getChapterItems(int catId) {
		ArrayList<ChapterItem> stories = new ArrayList<ChapterItem>();
		openDatabase();
		try {
			if (mSQLiteDb != null) {
				Cursor c = mSQLiteDb.query(TABLE_STORY, new String[] { "id", "catId", "title" }, " catId="+catId, null, null, null, null);
				if (c.moveToFirst()) {
					do {
						ChapterItem story = new ChapterItem();
						story.setId(c.getInt(c.getColumnIndex("id")));
						story.setCatId(c.getInt(c.getColumnIndex("catId")));
						story.setTitle(decrypt(c.getString(c.getColumnIndex("title"))));
						stories.add(story);
					} while (c.moveToNext());
				}
			}
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			close();
		}
		return stories;
	}

	@Override
	public List<ChapterItem> getChapterItems() {
		return getChapterItems(0);
	}

	@Override
	public ChapterDetail getChapterDetailById(int id) {
		ChapterDetail chapterDetail = null;
		openDatabase();
		try {
			if (mSQLiteDb != null) {
				Cursor c = mSQLiteDb.query(TABLE_STORY, new String[] { "id", "detail", "catId" }, " id="+id, null, null, null, null);
				if (c.moveToFirst()) {
					chapterDetail = new ChapterDetail();
					chapterDetail.setId(c.getInt(c.getColumnIndex("id")));
					chapterDetail.setDetail(decrypt(c.getString(c.getColumnIndex("detail"))));
					chapterDetail.setCatId(c.getInt(c.getColumnIndex("catId")));
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			close();
		}
		return chapterDetail;
	}

	@Override
	public List<Category> getCategories() {
		ArrayList<Category> categories = new ArrayList<Category>();
		openDatabase();
		try {
			if (mSQLiteDb != null) {
				Cursor c = mSQLiteDb.query(TABLE_CATEGORY, new String[] { "catId", "catName", "root" }, null, null, null, null, null);
				if (c.moveToFirst()) {
					do {
						Category cat = new Category();
						cat.setCatId(c.getInt(c.getColumnIndex("catId")));
						cat.setCatName(decrypt(c.getString(c.getColumnIndex("catName"))));
						cat.setRoot(c.getInt(c.getColumnIndex("root")));
						categories.add(cat);
					} while(c.moveToNext());
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			close();
		}
		return categories;
	}

	@Override
	public Category getCategoryById(int catId) {
		Category cat = null;
		openDatabase();
		try {
			if (mSQLiteDb != null) {
				Cursor c = mSQLiteDb.query(TABLE_CATEGORY, new String[] { "catId", "catName", "root" }, " catId="+catId, null, null, null, null);
				if (c.moveToFirst()) {
					cat = new Category();
					cat.setCatId(c.getInt(c.getColumnIndex("catId")));
					cat.setCatName(decrypt(c.getString(c.getColumnIndex("catName"))));
					cat.setRoot(c.getInt(c.getColumnIndex("root")));
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			close();
		}
		return cat;
	}
}
